"""
MainWindow module
"""

# pylint: disable=no-name-in-module, invalid-name, attribute-defined-outside-init

from PyQt5.QtWidgets import QMainWindow

from PyQt5.QtWidgets import (
    QWidget,
    QAction,
    QStatusBar,
    QLineEdit,
    QPushButton,
    QListWidget,
    QListWidgetItem,
    QTextEdit,
    QHBoxLayout,
    QVBoxLayout,
    QFileDialog,
)


from db_utils import get_books


class MainWindow(QMainWindow):
    """Main window"""

    def __init__(self):
        super().__init__()

        self.setWindowTitle('Bibliophile')
        self.setGeometry(100, 100, 800, 600)

        self._init_ui()

    def _init_ui(self):
        # search area
        self.searchEdit = QLineEdit(self)
        self.searchEdit.setPlaceholderText('Search')
        self.searchEdit.textChanged.connect(self._on_search_edit_text_changed)
        self.searchEdit.setFocus()

        self.searchButton = QPushButton('Search', self)
        self.searchButton.clicked.connect(self._on_search_button_clicked)

        self.layout1 = QHBoxLayout(self)
        self.layout1.addWidget(self.searchEdit)
        self.layout1.addWidget(self.searchButton)

        # files area
        self.booksArea = QListWidget(self)
        self.booksArea.clicked.connect(self._on_books_area_clicked)
        self.textArea = QTextEdit(self)

        self.layout2 = QHBoxLayout(self)
        self.layout2.addWidget(self.booksArea)
        self.layout2.addWidget(self.textArea)

        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.addLayout(self.layout1)
        self.mainLayout.addLayout(self.layout2)

        # menu bar
        self._init_menuBar()

        # status bar
        self._init_statusBar()

        w = QWidget()
        w.setLayout(self.mainLayout)
        self.setCentralWidget(w)

    def _init_menuBar(self):
        # actions
        action_open = QAction('Open', self)
        action_open.triggered.connect(self._on_action_open)
        action_quit = QAction('Quit', self)
        action_quit.triggered.connect(self._on_action_quit)

        # menus
        menu = self.menuBar()
        menu_file = menu.addMenu('File')
        menu_file.addAction(action_open)
        menu_file.addAction(action_quit)

    def _init_statusBar(self):
        self.statusBar = QStatusBar(self)
        self.setStatusBar(self.statusBar)

    def _on_action_open(self):
        # todo open index database
        filename, _ = QFileDialog.getOpenFileName(self)
        if filename:
            books = get_books(filename)
            for book_id, title in books:
                book_item = QListWidgetItem(title)
                book_item.setData(1, book_id)
                self.booksArea.addItem(book_item)

    def _on_books_area_clicked(self):
        item = self.booksArea.currentItem()
        print(item.data(1))

    def _on_action_quit(self):
        self.close()

    def _on_search_edit_text_changed(self):
        # really nothing yet
        pass

    def _on_search_button_clicked(self):
        # update files and text area
        pass
